import os

import pandas as pd

from common_operation.project_conf import PROJECT_DIR


class ParsePandaRobot(object):
    def __init__(self):
        self.data_robot = None
        self.data_path = None

    def init_data_robot(self, xlsx_path, choose_cols):
        """初始化原始数据加载"""
        self.data_path = xlsx_path
        self.data_robot = pd.read_excel(io=xlsx_path, header=0, usecols=choose_cols)

    def get_clean_tag_data(self, tag_id, start_datetime, end_datetime, suffix_mark=""):
        """
        从全量数据中，抽取出每个标签测试的干净数据
        :param tag_id:
        :param start_datetime:
        :param end_datetime:
        :param suffix_mark: 后缀标记文字
        :return:
        """
        basename = f"""{tag_id}_{start_datetime.strftime("%Y%m%d_%H%M%S")}_{end_datetime.strftime("%Y%m%d_%H%M%S")}{suffix_mark}.xlsx"""
        save_xslx_path = os.path.join(PROJECT_DIR['clean'], basename)
        try:
            clean_data = self.data_robot[(self.data_robot['tag'] == tag_id) & (self.data_robot['create_time'] >= start_datetime) & (self.data_robot['create_time'] < end_datetime) & (self.data_robot['filter'] == 1)].iloc[:, :]
        except:
            clean_data = self.data_robot[(self.data_robot['tag'] == tag_id) & (self.data_robot['create_time'] >= start_datetime) & (self.data_robot['create_time'] < end_datetime)].iloc[:, :]
        clean_data.to_excel(save_xslx_path)
        return basename

    def get_old_clean_tag_data(self, tag_id, start_datetime, end_datetime, suffix_mark=""):
        """
        旧版本，未加滤波时的数据抽取
        """
        basename = f"""{tag_id}_{start_datetime.strftime("%Y%m%d_%H%M%S")}_{end_datetime.strftime("%Y%m%d_%H%M%S")}{suffix_mark}.xlsx"""
        save_xslx_path = os.path.join(PROJECT_DIR['clean'], basename)
        clean_data = self.data_robot[(self.data_robot['tag'] == tag_id) & (self.data_robot['create_time'] >= start_datetime) & (self.data_robot['create_time'] < end_datetime)].iloc[:, :]
        clean_data.to_excel(save_xslx_path)
        return basename
